package mofei.concurrent;

import mofei.concurrent.task.*;
import mofei.concurrent.utils.DemoHolder;

import java.util.concurrent.*;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        ThreadPoolExecutor executor=new ThreadPoolExecutor(100,1000,10,TimeUnit.SECONDS,
                new LinkedBlockingDeque<Runnable>(1000),new CustomThreadFactory());
//        SleepTask sleepTask=new SleepTask();
//        WaitTask waitTask=new WaitTask();
//        WaitForTimeTask waitForTimeTask=new WaitForTimeTask();
//        executor.execute(sleepTask);
//        executor.execute(waitTask);
//        executor.execute(waitForTimeTask);
//
//        BlockedTask1 blockedTask1=new BlockedTask1();
//        BlockedTask2 blockedTask2=new BlockedTask2();
//        executor.execute(blockedTask1);
//        executor.execute(blockedTask2);
//
//        BlockedTask3 blockedTask3=new BlockedTask3();
//        BlockedTask4 blockedTask4=new BlockedTask4();
//        executor.execute(blockedTask3);
//        executor.execute(blockedTask4);
//
//        for (int i = 0; i < 100; i++) {
//            LockTask lockTask=new LockTask();
//            executor.execute(lockTask);
//        }
//
//        try {
//            DemoHolder.countDownLatch.await();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        System.out.println("执行结果是否正确：");
//        System.out.println(DemoHolder.isRight());
        MapAddTask addTask=new MapAddTask();
        MapForeach foreachTask=new MapForeach();
        executor.submit(addTask);
        executor.submit(foreachTask);

    }


    static class CustomThreadFactory implements ThreadFactory{
        public final String PREFIX_NAME="mofei-concurrent-";
        public volatile int threadNum=0;
        @Override
        public Thread newThread(Runnable r) {
            Thread t=new Thread(r);
            t.setName(PREFIX_NAME+(threadNum++));
            return t;
        }
    }
}
